package gui;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.swing.JPanel;
import Entities.ExecutionEntity;
import Entities.RequestEntity;
import common.ChatIF;
import common.CommonMethods;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;

public class ExecutePanel extends JPanel implements ChatIF {
	private ExecutionLeaderPanel eleader;
	private DeniedExecutionsPanel deniedExecutions;
	private AcademicFrame frame;
	private String pid;
	private ExecutePanel myself;
	private JComboBox <String> comboBox;
	private String requestNum;
	private JTextArea textArea;
	private ViewRequestPanel viewRequest;
	private JTextField deadLine;
	private JButton btnExtendDeadline;
	private String rid;
	private RequestEntity myEnt;
	private String myStatus;
	private JButton btnTempSave;
	private JButton btnSubmit;
	private JButton btnViewRequest;
	private ArrayList<Object> message;
	private static int flag;
	private final JButton btnDeniedExecutions = new JButton("Denied Executions");
	private ArrayList<Object> listofExecutionRequests;
	
	public ExecutePanel(AcademicFrame frame,ExecutionLeaderPanel eleader,String pid) {
		this.myself=this;
		this.pid=pid;
		this.eleader=eleader;
		this.frame=frame;
		this.flag=0;
		initialize();
	}
	
	public void initialize()
	{
		setLayout(null);
		JLabel lblExecute = new JLabel("Execution");
		lblExecute.setFont(new Font("David", Font.BOLD, 20));
		lblExecute.setBounds(25, 21, 123, 34);
		add(lblExecute);
		JLabel lblRemarks = new JLabel("Remarks:");
		lblRemarks.setBounds(25, 96, 62, 14);
		add(lblRemarks);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(97, 97, 318, 127);
		add(scrollPane);
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(eleader);
			}
		});
		btnCancel.setBounds(118, 364, 94, 23);
		add(btnCancel);
		btnSubmit = new JButton("Execute");
		btnSubmit.setEnabled(false);
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (requestNum.equalsIgnoreCase("--Choose a request--")) // filter invalid choice
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				else if (textArea.getText().length()==0)
					(new Dialog("Please enter remarks.")).setVisible(true);
				else if ((deadLine.getText().isEmpty())||(deadLine.getText().equals("Not yet set")) )
					(new Dialog("There is no deadline to evaluate exceeding , Contact your Admin.")).setVisible(true);
				else{
					message = new ArrayList<Object>();
					String code = "enterNewExecutionRecord";
					String newStatus = "11";
					String bool = "no";
					String request = requestNum;
					String det = textArea.getText();
					message.add(code);
					message.add(request);
					message.add(det);
					message.add(bool);
					message.add(newStatus);
						if (CommonMethods.exceededDeadline(deadLine.getText()))
						{
							String code1 = "fillInExceeded";
							message.add(code1);
							int number = CommonMethods.getExceededTime(myEnt.getDeadline());
							message.add(number);
							myEnt.setrid(Integer.parseInt(requestNum));
							message.add(myEnt);
							message.add(myStatus);
						}
						else {
							message.add("nothing");
						}
					AcademicFrame.myclient.sendMessageToServer(message);
				}
			}
		});
		btnSubmit.setBounds(301, 364, 101, 23);
		add(btnSubmit);
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
				requestNum = (String)cb.getSelectedItem();
				if(!requestNum.equalsIgnoreCase("--Choose a request--"))
				{
					textArea.setText("");
					btnTempSave.setEnabled(true);
					btnSubmit.setEnabled(true);
					btnViewRequest.setEnabled(true);
					ArrayList<Object> message = new ArrayList<Object>();
					String choosenID = (String)comboBox.getSelectedItem();
					String code = "trackRequest+";
					message.add(code);
					message.add(choosenID);
					AcademicFrame.myclient.sendMessageToServer(message);
				}
			}
		});
		comboBox.setBounds(129, 66, 286, 20);
		add(comboBox);
		JLabel lblNewLabel = new JLabel("Request number :");
		lblNewLabel.setBounds(25, 69, 107, 14);
		add(lblNewLabel);
		btnViewRequest = new JButton("View Request");
		btnViewRequest.setEnabled(false);
		btnViewRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if (requestNum.equalsIgnoreCase("--Choose a request--")) // filter invalid choice
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				else{
				viewRequest = new ViewRequestPanel(requestNum,frame,myself,false,"");
				frame.setSize(550, 730);	
				frame.setContentPane(viewRequest);
				}
			}
		});
		btnViewRequest.setBounds(330, 235, 107, 23);
		add(btnViewRequest);

		btnExtendDeadline = new JButton("Extend Deadline");
		btnExtendDeadline.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (requestNum.equalsIgnoreCase("--Choose a request--")) // filter invalid choice
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				else if (deadLine.getText().isEmpty() ||deadLine.getText().equals("Not yes set"))
				{
					(new Dialog ("The deadline is not set yet !")).setVisible(true);
				}
				else if (CommonMethods.exceededDeadline(deadLine.getText()))
				{	
					(new Dialog ("Deadline has passed, cannot extend.")).setVisible(true);
				}
				else
					if (CommonMethods.moreThan3Days(deadLine.getText()) )
					{
						(new Dialog ("More than 3 days to the deadline, cannot extend.")).setVisible(true);
					}
					else
					{
						rid = comboBox.getSelectedItem().toString();
						comboBox.setEnabled(false);
						ExtendDeadline extendFrame = new ExtendDeadline(deadLine.getText(), btnExtendDeadline, rid, "10", "executed", "wasExtended", "rid1" );
						extendFrame.setVisible(true);
						extendFrame.setSize(760,400);
					}
				}
		});
		btnExtendDeadline.setBounds(302, 280, 123, 23);
		add(btnExtendDeadline);
		JPanel panel = new JPanel();
		panel.setBounds(152, 281, 140, 20);
		add(panel);
		panel.setLayout(null);
		deadLine = new JTextField();
		deadLine.setBounds(0, 0, 140, 20);
		panel.add(deadLine);
		deadLine.setEditable(false);
		deadLine.setColumns(10);
		JLabel lblDeadLine = new JLabel("Dead line:");
		lblDeadLine.setBounds(92, 284, 56, 14);
		add(lblDeadLine);
		btnTempSave = new JButton("temp save");
		btnTempSave.setEnabled(false);
		btnTempSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (requestNum.equalsIgnoreCase("--Choose a request--")) // filter invalid choice
					(new Dialog("Please choose a request from the list first.")).setVisible(true);
				else if (textArea.getText().length()==0)
					(new Dialog("Please enter remarks.")).setVisible(true);
				else{
					ArrayList<Object> message = new ArrayList<Object>();
					String code = "getExecutedEntity";
					String bool = "no";
					String request = requestNum;
					String det = textArea.getText();
					message.add(code);
					message.add(request);
					message.add(det);
					message.add(bool);
					AcademicFrame.myclient.sendMessageToServer(message);
				}
				
			}
		});
		btnTempSave.setBounds(201, 235, 119, 23);
		add(btnTempSave);
		JLabel lblNotice = new JLabel("* notice : in order to use the Extend button , you need to create\r\n");
		lblNotice.setFont(new Font("David", Font.PLAIN, 12));
		lblNotice.setVerticalAlignment(SwingConstants.TOP);
		lblNotice.setBounds(25, 314, 318, 14);
		add(lblNotice);
		JLabel lblAnExecutionRecord = new JLabel(" an execution record first by adding your details and saving it .");
		lblAnExecutionRecord.setFont(new Font("David", Font.PLAIN, 12));
		lblAnExecutionRecord.setBounds(25, 328, 318, 14);
		add(lblAnExecutionRecord);
		btnDeniedExecutions.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {		
				deniedExecutions = new DeniedExecutionsPanel(frame,myself,pid);
				listofExecutionRequests = new ArrayList<Object>();
				listofExecutionRequests.add("getDeniedExecutions");
				listofExecutionRequests.add(pid);
				listofExecutionRequests.add("122");
				AcademicFrame.myclient.sendMessageToServer(listofExecutionRequests);
				frame.setSize(500,450);
				frame.setContentPane(deniedExecutions);
			}
		});
		btnDeniedExecutions.setBounds(51, 235, 140, 23);
		add(btnDeniedExecutions);
	}

	@Override
	public void display(Object message) {
		ArrayList<Object> listofExRequests=new ArrayList<Object>();
		ArrayList<Object> TaskDuration=new ArrayList<Object>();
		ArrayList<Object> exReturn=new ArrayList<Object>();
		listofExRequests.clear(); // clear the info array list
		TaskDuration.clear(); // clear the info array list
		listofExRequests = (ArrayList<Object>)message;
		ExecutionEntity ex;
		
		TaskDuration = (ArrayList<Object>)message;
		System.out.println("in display");
		if (((String)(listofExRequests.get(0))).equalsIgnoreCase("listOfExRequests"))
		{
			ArrayList<String> assessorRequests = (ArrayList<String>)message; 				
			int numberOfSystems = Integer.parseInt((assessorRequests.get(1)).toString()); // get the number of systems in the list
			this.comboBox.addItem("--Choose a Request--");
			for (int i=0; i < numberOfSystems ; i++ )
			{
				this.comboBox.addItem((String)assessorRequests.get(i+2));  // add systems to the list
			}
		}
		else if(((String)(listofExRequests.get(0))).equalsIgnoreCase("enterNewExecutionRecord"))
			{
			 if(!listofExRequests.get(2).toString().equals(null))
				{
				exReturn = (ArrayList<Object>) listofExRequests.get(2);
				if(((String)exReturn.get(1)).equalsIgnoreCase("Valid"))
				{
					//exReturn = (ArrayList<Object>) listofExRequests.get(2);
					(new Dialog("The request was Exceeded there so it is documented for future notice !")).setVisible(true);
				}
				else{}
				}
				(new Dialog("The request was Ducumented !")).setVisible(true);
				frame.setContentPane(eleader);
			}
		else if (((String)(listofExRequests.get(0))).equalsIgnoreCase("requestData"))
			{
				myEnt = (RequestEntity)listofExRequests.get(1);
				ArrayList<Object> extra = new ArrayList<Object>();
				extra = (ArrayList<Object>) listofExRequests.get(2);
				if(extra.get(1).equals("Valid"))
				{
					ExecutionEntity mySavedExecutionDetails = (ExecutionEntity) extra.get(2);
					textArea.append(mySavedExecutionDetails.getExecutionDetails());
					if((mySavedExecutionDetails.getWasExtended().toString()).equals("yes"))
					{
					btnExtendDeadline.setEnabled(false);
					flag =1;
					}		
				}
				myStatus=myEnt.getStatusid();
				myEnt.setStatus(myStatus);
				deadLine.setText(myEnt.getDeadline());	

			 if (deadLine.getText().isEmpty() ||deadLine.getText().equals("Not yet set") ||deadLine.getText().equals("NULL") )
				{
					btnExtendDeadline.setEnabled(false);
				}
				else if (CommonMethods.exceededDeadline(deadLine.getText()))
				{	
					btnExtendDeadline.setEnabled(false);
				}
				else if (CommonMethods.moreThan3Days(deadLine.getText()) )
					{
						btnExtendDeadline.setEnabled(false);
					}
				else {
							if((flag == 0))
							{
								btnExtendDeadline.setEnabled(true);
								
							}
							flag = 0 ;
					}
			 if(extra.get(1).equals("notValid"))
			 btnExtendDeadline.setEnabled(false);

			}
		else if (((String)(listofExRequests.get(0))).equalsIgnoreCase("getExecutedEntity"))
		{
			ArrayList<Object> messageToSend = new ArrayList<Object>();
			if(listofExRequests.get(1).equals("Valid"))
			{
				String code = "updateExecutedReq";
				String textarea = textArea.getText();
				messageToSend.add(code);
				messageToSend.add(requestNum);
				messageToSend.add(textarea);
				AcademicFrame.myclient.sendMessageToServer(messageToSend);
			}
			else
			{
				String code = "insertNewExecutionReqForSave";
				String textarea = textArea.getText();
				String bool = "no";
				messageToSend.add(code);
				messageToSend.add(requestNum);
				messageToSend.add(textarea);
				messageToSend.add(bool);
				AcademicFrame.myclient.sendMessageToServer(messageToSend);
			}
		}
			else if (((String)(listofExRequests.get(0))).equalsIgnoreCase("insertNewExecutionReqForSave"))
			{
				
				////////////////
				(new Dialog("The request was saved !")).setVisible(true);
				frame.setContentPane(eleader);
			}
			else if (((String)(listofExRequests.get(0))).equalsIgnoreCase("updateExecutedReq")){
				
				if(((String)(listofExRequests.get(1))).equals("Valid")){
					(new Dialog("The request was Updated !")).setVisible(true);
					frame.setContentPane(eleader);
				}
				else (new Dialog("Failed to update .")).setVisible(true);
				frame.setContentPane(eleader);
			}
}
}








